using KrasDD;
using KrasDD.Data;
using KrasDD.Services.Implementations;
using KrasDD.Services.Interfaces;
using System.Text;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        var fsql = AppContainer.CreateFreeSql(builder);
        builder.Services.AddSingleton<IFreeSql>(fsql);


        // 添加Redis缓存服务
        builder.Services.AddStackExchangeRedisCache(options =>
        {
            options.Configuration = builder.Configuration.GetConnectionString("Redis");
            options.InstanceName = "APP:";
        });

        // 添加通用服务
        builder.Services.AddScoped(typeof(IBaseService<>), typeof(BaseService<>));

        // 添加具体服务
        builder.Services.AddScoped<IIdentityService, IdentityService>();
        builder.Services.AddScoped<IAccessService, AccessService>();
        builder.Services.AddScoped<IAliasService, AliasService>();
        builder.Services.AddScoped<IDocumentFileService, DocumentFileService>();
        builder.Services.AddScoped<IDocumentService, DocumentService>();
        builder.Services.AddScoped<IFileService, FileService>();
        builder.Services.AddScoped<IMemberService, MemberService>();
        builder.Services.AddScoped<IPropertyService, PropertyService>();
        builder.Services.AddScoped<IServerEventService, ServerEventService>();
        builder.Services.AddScoped<IItemtypeService, ItemtypeService>();
        builder.Services.AddScoped<IMethodService, MethodService>();
        builder.Services.AddScoped<IPermissionService, PermissionService>();
        builder.Services.AddScoped<ITeamService, TeamService>();
        
        // 添加事件处理器服务
        builder.Services.AddScoped<IEventHandler, ServerEventHandler>();
        
        builder.Services.AddScoped<SeedDataInitializer>();
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();

        // 添加全局过滤器
        builder
            .Services.AddControllers(options =>
            {
                options.Filters.Add<KrasDD.Filters.ApiResponseFilter>();
            })
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                options.SerializerSettings.Converters.Add(
                    new Newtonsoft.Json.Converters.StringEnumConverter()
                );
            });

        builder.Services.AddMvc(options =>
        {
            options.EnableEndpointRouting = false;
        });

        var app = builder.Build();
        AppContainer.ServiceProvider = app.Services;

        // 初始化种子数据
        using (var scope = app.Services.CreateScope())
        {
            var seedDataInitializer =
                scope.ServiceProvider.GetRequiredService<SeedDataInitializer>();
            seedDataInitializer.InitializeAsync().Wait();
        }

        app.UseSwagger();
        app.UseSwaggerUI();
        app.MapControllers();
        app.UseMvcWithDefaultRoute();

        app.MapGet("/", () => Results.Redirect("/swagger"));

        app.Run();
    }
}
